package dbutil.dao;

import dbutil.model.ClassInfo;
import dbutil.utils.DBUtil;
import edu.nf.SQLExecutor;
import edu.nf.handler.BeanHandler;

import java.sql.SQLException;

public class ClassDao {

    public int save(ClassInfo cla){
        String sql = "INSERT INTO CLASS_INFO VALUES(?,?)";
        SQLExecutor se = new SQLExecutor(DBUtil.getConnection());
        try {
            return se.executeUpdate(sql, cla.getCid(), cla.getCname());
        } catch (SQLException e) {
            throw new RuntimeException("Exception in save.", e);
        }
    }

    public ClassInfo findClassById(String cid){
        String sql = "SELECT * FROM CLASS_INFO WHERE C_ID = ?";
        SQLExecutor se = new SQLExecutor(DBUtil.getConnection());
        BeanHandler<ClassInfo> handler = new BeanHandler<>(ClassInfo.class);
        try {
            return se.executeQuery(sql, handler, cid);
        } catch (SQLException e) {
            throw new RuntimeException("Exception in findClassById.", e);
        }
    }
}
